using System;
using System.Drawing;
using System.Drawing.Design;
using System.Collections;
using System.ComponentModel;

namespace Camalon.Drawing
{

    [Editor("Camalon.Drawing.Design.ShapeCollectionEditor, Camalon.Drawing.Design", typeof(UITypeEditor))]
    public class ShapeCollection : System.Collections.CollectionBase, IDisposable
	{
		// ---------------------------------------------------------------------------------------------
		// Important NOTE!!!!: Implenting IClonable interface causes Visual inheritance to die!!!!
		// --------------------------------------------------------------------------------------------


//		[Browsable(true)]
//		public event EventHandler ShapeInternalChanged		= null;
//
//		[Browsable(true)]
//		public event ValueChangeEventHandler ShapeBoundsChanged = null;
//
//		[Browsable(true)]
//		public event ValueChangeEventHandler ShapePathBoundsChanged = null;

		[Browsable(true)]
		public event ShapeCollectionEventHandler CollectionChanged		= null;


		#region IDisposable Members

		public void Dispose()
		{
//			ShapeInternalChanged	= null;
//			ShapeBoundsChanged		= null;
//			ShapePathBoundsChanged	= null;
			CollectionChanged		= null;
		}

		#endregion IDisposable Members

		#region Event Invokers

//		internal void OnShapeChanged(object sender, EventArgs e)
//		{	
//			if( ShapeInternalChanged != null) ShapeInternalChanged(sender,e);
//		}
//
//		internal void OnShapeBoundsChanged(object sender, ValueChangeEventArgs e)
//		{	
//			if( ShapeBoundsChanged!=null) ShapeBoundsChanged(sender,e);
//		}
//
//		internal void OnShapePathBoundsChanged( object sender, ValueChangeEventArgs e)
//		{
//			if( ShapePathBoundsChanged !=null ) ShapePathBoundsChanged(sender,e);
//		}
//
		#endregion Event Invokers

//		private void AddShapeEvents( Shape shape )
//		{
//			shape.BoundsChanged			+= new ValueChangeEventHandler(OnShapeBoundsChanged);
//			shape.InternalChanged		+= new EventHandler(OnShapeChanged);
//			shape.PathBoundsChanged		+= new ValueChangeEventHandler(OnShapePathBoundsChanged);
//		}
//
//		private void RemoveShapeEvents( Shape shape )
//		{
//			shape.BoundsChanged			-= new ValueChangeEventHandler(OnShapeBoundsChanged);
//			shape.InternalChanged		-= new EventHandler(OnShapeChanged);
//			shape.PathBoundsChanged		-= new ValueChangeEventHandler(OnShapePathBoundsChanged);
//		}


		protected internal virtual bool ShouldSerialize()
		{
			return this.Count > 0;
		}


		public Shape this[ int index ]  
		{
			get  {	return( (Shape) List[index] ); }
			//set  {	List[index] = value; }
		}

		public void AddRange( Shape[] value )  {foreach( Shape shape in value) this.Add( shape );}
		public int  Add( Shape value) 
		{	if( !Contains(value) )
			return List.Add( value);
			else return -1;
		}
		public void CopyTo( Shape[] array, int arrayStartIndex){	List.CopyTo( array, arrayStartIndex);	}
		public void Insert( int index, Shape value){ List.Insert(index, value);}
		public int  IndexOf( Shape value) { return List.IndexOf(value);}
		public bool Contains( Shape value) { return List.Contains(value);}
		public void Remove( Shape value) { List.Remove(value);}
		public Shape[] ToArray()
		{
			Shape[] shapes = new Shape[this.Count];

			
			for( int i= 0; i< this.Count; i++)
				shapes[i]= this[i].Clone() as Shape; 
			return shapes;

		}
		
		
		[DefaultValue( true)]
		[Browsable( false)]
		public bool IsReadOnly
		{
			get
			{
				return true;
			}
		}


		#region COLLECTION EVENT OVERRIDES
		
		protected override void OnInsertComplete(int index, object value)
		{
			
			if( CollectionChanged != null) CollectionChanged( this, new ShapeCollectionEventArgs(index, (Shape)value,false));

//			Shape shape = value as Shape;
//			if( shape != null ) AddShapeEvents( shape );

			base.OnInsertComplete (index, value);
		}

		protected override void OnRemoveComplete(int index, object value)
		{
			if( CollectionChanged != null) CollectionChanged( this, new ShapeCollectionEventArgs(index, (Shape)value,true));

//			Shape shape = value as Shape;
//			if( shape != null ) RemoveShapeEvents( shape);

			base.OnRemoveComplete (index, value);
		}

		protected override void OnSetComplete(int index, object oldValue, object newValue)
		{
			if( CollectionChanged != null) CollectionChanged( this, new ShapeCollectionEventArgs(index, (Shape)oldValue,(Shape)newValue));

//			Shape oldShape = oldValue as Shape;
//			if( oldShape != null ) RemoveShapeEvents( oldShape );
//
//			Shape newShape = newValue as Shape;
//			if( newShape != null ) AddShapeEvents( newShape);
			
			base.OnSetComplete (index, oldValue, newValue);
		}

		
		protected override void OnClearComplete()
		{
			if( CollectionChanged != null) CollectionChanged( this, new ShapeCollectionEventArgs());

//			foreach( Shape shape in List) RemoveShapeEvents( shape );

			base.OnClearComplete ();
		}

		
		#endregion EVENT OVERRIDES

	}




}
